package com.zhangzy.qm.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zhangzy.qm.dao.UserDao;
import com.zhangzy.qm.entity.User;
import com.zhangzy.qm.util.HttpUtil;

public class UserController extends ActionSupport {
	private HttpServletRequest request = ServletActionContext.getRequest();
	private HttpServletResponse response = ServletActionContext.getResponse();
	private UserDao userDao;
	
	private File uploadify;	 
	private String uploadifyFileName;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public String toLogin() {
		return "login";
	}

	public String doLogin() {
		String userName = HttpUtil.getString(request, "userName");
		String password = HttpUtil.getString(request, "password");
		User user = userDao.getUserByParam(userName, password);
		if (user != null && user.getId() > 0) {
			request.getSession().setAttribute("user", user);
			return "dataIndex";
		} else {
			request.setAttribute("error_msg", "用户名或密码错误");
			request.setAttribute("userName", userName);
			return "login";
		}

	}

	public void logout() {
		HttpSession session = request.getSession();
		session.removeAttribute("userName");
		StringBuilder sb = new StringBuilder();
		sb.append("{'success':'true'}");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			if (sb != null) {
				out.println(sb.toString());
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	 public File getUploadify() {
	  return uploadify;
	 }


	 public void setUploadify(File uploadify) {
	  this.uploadify = uploadify;
	 }


	 public String getUploadifyFileName() {
	  return uploadifyFileName;
	 }


	 public void setUploadifyFileName(String uploadifyFileName) {
	  this.uploadifyFileName = uploadifyFileName;
	 }		
}
